﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Particle_Editor
{
    public partial class Form1 : Form
    {
        bool looping = true;
        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public int ID = -1;

        public String SSystemPath { get; set; }
        public String SOpenFile { get; set; }

        AddEditEmitterForm addForm;
        ColorDialog colorPick;
        DateTime previousTime = System.DateTime.Now;

        SGP.CSGP_Direct3D D3D = SGP.CSGP_Direct3D.GetInstance();
        SGP.CSGP_TextureManager TM = SGP.CSGP_TextureManager.GetInstance();


        List<Emitter> emitters = new List<Emitter>();

        public Form1()
        {
            InitializeComponent();
            D3D.Initialize(splitContainer1.Panel1, true);
            D3D.AddRenderTarget(splitContainer1.Panel2);
            TM.Initialize(D3D.Device, D3D.Sprite);
            Disabled(true);
            if (System.IO.File.Exists("peFilePath.txt"))
            {
                System.IO.StreamReader reader = new System.IO.StreamReader("peFilePath.txt");
                SSystemPath = reader.ReadLine();
                reader.Close();

                if (!System.IO.Directory.Exists(SSystemPath))
                {
                    FolderBrowserDialog dlg = new FolderBrowserDialog();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        SSystemPath = dlg.SelectedPath;
                    }
                }
            }
            else
            {
                    FolderBrowserDialog dlg = new FolderBrowserDialog();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        SSystemPath = dlg.SelectedPath;
                    }
            }
        }

        public new void Update()
        {
            
            DateTime currentTime = System.DateTime.Now;
            TimeSpan time = currentTime - previousTime;
            previousTime = currentTime;
            float dt = (float)time.TotalSeconds;
            if (emitterList.SelectedIndex > -1)
                emitters[emitterList.SelectedIndex].Update(dt);
        }

        public void Render()
        {
            D3D.Clear(splitContainer1.Panel1, Color.Black);
            D3D.DeviceBegin();
            D3D.SpriteBegin();

            if (emitterList.SelectedIndex != -1)
            {
                for (int i = 0; i < emitters[emitterList.SelectedIndex].liveList.Count; ++i)
                {
                    if (emitters[emitterList.SelectedIndex].ImageID != -1)
                    {
                        TM.Draw(emitters[emitterList.SelectedIndex].ImageID,
                            (int)emitters[emitterList.SelectedIndex].liveList[i].Position.X,
                            (int)emitters[emitterList.SelectedIndex].liveList[i].Position.Y,
                            emitters[emitterList.SelectedIndex].liveList[i].FScaleCurrent,
                            emitters[emitterList.SelectedIndex].liveList[i].FScaleCurrent,
                            emitters[emitterList.SelectedIndex].flyweight.drawRect,
                            emitters[emitterList.SelectedIndex].flyweight.centerPoint.X,
                            emitters[emitterList.SelectedIndex].flyweight.centerPoint.Y,
                            emitters[emitterList.SelectedIndex].liveList[i].FRotationCurrent,
                            emitters[emitterList.SelectedIndex].liveList[i].CColor);
                    }
                }
            }
            D3D.SpriteEnd();
            D3D.DeviceEnd();
            D3D.Present();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Looping = false;
            System.IO.StreamWriter writer = new System.IO.StreamWriter("peFilePath.txt");
            writer.WriteLine(SSystemPath);
            writer.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (emitterList.SelectedIndex != -1)
            {
                Disabled(false);

                numParticlesNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].IMaxParticles;
                spawnRateNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].FSpawnRate;
                gravityCheckBox.Checked = emitters[emitterList.SelectedIndex].flyweight.BGravity;
                windCheckBox.Checked = emitters[emitterList.SelectedIndex].flyweight.BWind;
                repeatCheckBox.Checked = emitters[emitterList.SelectedIndex].BRepeat;
                switch (emitters[emitterList.SelectedIndex].flyweight.IEmitterType)
                {
                    case (int)Flyweight.EmitterType.POINT:
                        {
                            pointRadioButton.Checked = true;
                            break;
                        }
                    case (int)Flyweight.EmitterType.LINE:
                        {
                            lineRadioButton.Checked = true;
                            break;
                        }
                    case (int)Flyweight.EmitterType.CIRCLE:
                        {
                            circleRadioButton.Checked = true;
                            break;
                        }
                    case (int)Flyweight.EmitterType.RECTANGLE:
                    default:
                        {
                            rectangleRadioButton.Checked = true;
                            break;
                        }
                }

                widthNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.IEmitterWidth;

                heightNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.IEmitterHeight;

                minLifeNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FMinLife;

                maxLifeNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FMaxLife;

                startSpeedNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FStartSpeed;

                endSpeedNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FEndSpeed;

                startScaleNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FStartScale;

                endScaleNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FEndScale;

                rotationNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FRotationChangePerSec;

                rangeNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.IEmitterRange;

                weightNumericUpDown.Value = (decimal)emitters[emitterList.SelectedIndex].flyweight.FParticleMass;

                startAlphaNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FStartColorA * 255);
                startRedNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FStartColorR * 255);
                startGreenNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FStartColorG * 255);
                startBlueNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FStartColorB * 255);

                startColorButton.BackColor = Color.FromArgb((int)startAlphaNumericUpDown.Value, (int)startRedNumericUpDown.Value, (int)startGreenNumericUpDown.Value, (int)startBlueNumericUpDown.Value);

                endAlphaNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FEndColorA * 255);
                endRedNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FEndColorR * 255);
                endGreenNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FEndColorG * 255);
                endBlueNumericUpDown.Value = (int)(emitters[emitterList.SelectedIndex].flyweight.FEndColorB * 255);

                endColorButton.BackColor = Color.FromArgb((int)endAlphaNumericUpDown.Value, (int)endRedNumericUpDown.Value, (int)endGreenNumericUpDown.Value, (int)endBlueNumericUpDown.Value);
            }
            else
            {
                Disabled(true);
            }
        }

        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && emitterList.SelectedIndex != -1)
            {
                PointF pos = new PointF(e.X - 60, e.Y - 60);
                emitters[emitterList.SelectedIndex].flyweight.Position = pos;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1_FormClosing(sender, new FormClosingEventArgs(CloseReason.ApplicationExitCall, false));
            Dispose(true);
        }

        private void pointRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            widthLabel.Visible = !pointRadioButton.Checked;
            radiusLabel.Visible = !pointRadioButton.Checked;
            widthNumericUpDown.Visible = !pointRadioButton.Checked;
            heightLabel.Visible = !pointRadioButton.Checked;
            heightNumericUpDown.Visible = !pointRadioButton.Checked;
            if (pointRadioButton.Checked)
                emitters[emitterList.SelectedIndex].flyweight.IEmitterType = (int)Flyweight.EmitterType.POINT;
        }

        private void lineRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            widthLabel.Visible = lineRadioButton.Checked;
            radiusLabel.Visible = !lineRadioButton.Checked;
            widthNumericUpDown.Visible = lineRadioButton.Checked;
            heightLabel.Visible = !lineRadioButton.Checked;
            heightNumericUpDown.Visible = !lineRadioButton.Checked;
            if (lineRadioButton.Checked)
                emitters[emitterList.SelectedIndex].flyweight.IEmitterType = (int)Flyweight.EmitterType.LINE;
        }

        private void circleRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            widthLabel.Visible = !circleRadioButton.Checked;
            radiusLabel.Visible = circleRadioButton.Checked;
            widthNumericUpDown.Visible = circleRadioButton.Checked;
            heightLabel.Visible = !circleRadioButton.Checked;
            heightNumericUpDown.Visible = !circleRadioButton.Checked;
            if (circleRadioButton.Checked)
                emitters[emitterList.SelectedIndex].flyweight.IEmitterType = (int)Flyweight.EmitterType.CIRCLE;
        }

        private void rectangleRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            widthLabel.Visible = rectangleRadioButton.Checked;
            radiusLabel.Visible = !rectangleRadioButton.Checked;
            widthNumericUpDown.Visible = rectangleRadioButton.Checked;
            heightLabel.Visible = rectangleRadioButton.Checked;
            heightNumericUpDown.Visible = rectangleRadioButton.Checked;
            if (rectangleRadioButton.Checked)
                emitters[emitterList.SelectedIndex].flyweight.IEmitterType = (int)Flyweight.EmitterType.RECTANGLE;
        }

        private void emitterListAddButton_Click(object sender, EventArgs e)
        {
            addForm = new AddEditEmitterForm(SSystemPath);
            if (DialogResult.OK == addForm.ShowDialog())
            {
                Emitter em = new Emitter(addForm.file);
                em.ImageID = TM.LoadTexture(SSystemPath + "\\" + em.SFileName);
                if (-1 != em.ImageID)
                {
                    em.SEmitterName = addForm.name;
                    em.flyweight.centerPoint = addForm.point;
                    em.flyweight.drawRect = addForm.rect;
                    emitters.Add(em);
                    emitterList.Items.Add(new Item(addForm.name, em));
                    emitterList.SelectedIndex = emitters.Count - 1;
                }
                else
                {
                    MessageBox.Show("File path is not valid.");
                }
            }
        }

        public void Disabled(bool onOff)
        {
            numParticlesNumericUpDown.ReadOnly = onOff;
            spawnRateNumericUpDown.ReadOnly = onOff;
            gravityCheckBox.Enabled = !onOff;
            windCheckBox.Enabled = !onOff;
            repeatCheckBox.Enabled = !onOff;
            pointRadioButton.Enabled = !onOff;
            lineRadioButton.Enabled = !onOff;
            circleRadioButton.Enabled = !onOff;
            rectangleRadioButton.Enabled = !onOff;

            widthNumericUpDown.ReadOnly = onOff;
            heightNumericUpDown.ReadOnly = onOff;

            minLifeNumericUpDown.ReadOnly = onOff;

            maxLifeNumericUpDown.ReadOnly = onOff;

            startSpeedNumericUpDown.ReadOnly = onOff;

            endSpeedNumericUpDown.ReadOnly = onOff;

            startScaleNumericUpDown.ReadOnly = onOff;

            endScaleNumericUpDown.ReadOnly = onOff;

            rotationNumericUpDown.ReadOnly = onOff;

            rangeNumericUpDown.ReadOnly = onOff;

            weightNumericUpDown.ReadOnly = onOff;

            startAlphaNumericUpDown.ReadOnly = onOff;
            startRedNumericUpDown.ReadOnly = onOff;
            startGreenNumericUpDown.ReadOnly = onOff;
            startBlueNumericUpDown.ReadOnly = onOff;

            startColorButton.Enabled = !onOff;

            endAlphaNumericUpDown.ReadOnly = onOff;
            endRedNumericUpDown.ReadOnly = onOff;
            endGreenNumericUpDown.ReadOnly = onOff;
            endBlueNumericUpDown.ReadOnly = onOff;

            endColorButton.Enabled = !onOff;
        }

        private class Item
        {
            public string Name;
            public Emitter em;
            public Item(string name, Emitter emit)
            {
                Name = name;
                em = emit;
            }

            public override string ToString()
            {
                return Name;
            }
        }

        private void numParticlesNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].IMaxParticles = (int)numParticlesNumericUpDown.Value;
        }

        private void spawnRateNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].FSpawnRate = (float)spawnRateNumericUpDown.Value;
        }

        private void gravityCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.BGravity = gravityCheckBox.Checked;
        }

        private void windCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.BWind = windCheckBox.Checked;
        }

        private void repeatCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].BRepeat = repeatCheckBox.Checked;
        }

        private void widthNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.IEmitterWidth = (int)widthNumericUpDown.Value;
        }

        private void heightNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.IEmitterHeight = (int)heightNumericUpDown.Value;
        }

        private void minLifeNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FMinLife = (float)minLifeNumericUpDown.Value;
            if (maxLifeNumericUpDown.Value > minLifeNumericUpDown.Value + 10)
                maxLifeNumericUpDown.Value = minLifeNumericUpDown.Value + 10;
            else if (maxLifeNumericUpDown.Value < minLifeNumericUpDown.Value)
                maxLifeNumericUpDown.Value = minLifeNumericUpDown.Value;
            maxLifeNumericUpDown.Minimum = minLifeNumericUpDown.Value;
            maxLifeNumericUpDown.Maximum = (minLifeNumericUpDown.Value + 10);
        }

        private void maxLifeNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (maxLifeNumericUpDown.Value < minLifeNumericUpDown.Value)
                maxLifeNumericUpDown.Value = minLifeNumericUpDown.Value;
            emitters[emitterList.SelectedIndex].flyweight.FMaxLife = (float)(minLifeNumericUpDown.Value + maxLifeNumericUpDown.Value);
        }

        private void startSpeedNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FStartSpeed = (float)startSpeedNumericUpDown.Value;
        }

        private void endSpeedNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FEndSpeed = (float)endSpeedNumericUpDown.Value;
        }

        private void startScaleNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FStartScale = (float)startScaleNumericUpDown.Value;
        }

        private void endScaleNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FEndScale = (float)endScaleNumericUpDown.Value;
        }

        private void rotationNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FRotationChangePerSec = (float)rotationNumericUpDown.Value;
        }

        private void rangeNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.IEmitterRange = (int)rangeNumericUpDown.Value;
        }

        private void weightNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            emitters[emitterList.SelectedIndex].flyweight.FRotationChangePerSec = (float)weightNumericUpDown.Value;
        }

        private void startColorButton_Click(object sender, EventArgs e)
        {
            colorPick = new ColorDialog();
            if (DialogResult.OK == colorPick.ShowDialog())
            {
                startAlphaNumericUpDown.Value = colorPick.Color.A;
                startRedNumericUpDown.Value = colorPick.Color.R;
                startGreenNumericUpDown.Value = colorPick.Color.G;
                startBlueNumericUpDown.Value = colorPick.Color.B;
                emitters[emitterList.SelectedIndex].flyweight.FStartColorA = (float)(startAlphaNumericUpDown.Value / 255);
                emitters[emitterList.SelectedIndex].flyweight.FStartColorR = (float)(startRedNumericUpDown.Value / 255);
                emitters[emitterList.SelectedIndex].flyweight.FStartColorG = (float)(startGreenNumericUpDown.Value / 255);
                emitters[emitterList.SelectedIndex].flyweight.FStartColorB = (float)(startBlueNumericUpDown.Value / 255);
                startColorButton.BackColor = Color.FromArgb((int)startAlphaNumericUpDown.Value, (int)startRedNumericUpDown.Value, (int)startGreenNumericUpDown.Value, (int)startBlueNumericUpDown.Value);
            }
        }

        private void startAlphaNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            startColorButton.BackColor = Color.FromArgb((int)startAlphaNumericUpDown.Value, (int)startRedNumericUpDown.Value, (int)startGreenNumericUpDown.Value, (int)startBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FStartColorA = (float)(startAlphaNumericUpDown.Value / 255);
        }

        private void startRedNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            startColorButton.BackColor = Color.FromArgb((int)startAlphaNumericUpDown.Value, (int)startRedNumericUpDown.Value, (int)startGreenNumericUpDown.Value, (int)startBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FStartColorR = (float)(startRedNumericUpDown.Value / 255);
        }

        private void startGreenNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            startColorButton.BackColor = Color.FromArgb((int)startAlphaNumericUpDown.Value, (int)startRedNumericUpDown.Value, (int)startGreenNumericUpDown.Value, (int)startBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FStartColorG = (float)(startGreenNumericUpDown.Value / 255);
        }

        private void startBlueNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            startColorButton.BackColor = Color.FromArgb((int)startAlphaNumericUpDown.Value, (int)startRedNumericUpDown.Value, (int)startGreenNumericUpDown.Value, (int)startBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FStartColorB = (float)(startBlueNumericUpDown.Value / 255);
        }

        private void endColorButton_Click(object sender, EventArgs e)
        {
            colorPick = new ColorDialog();
            if (DialogResult.OK == colorPick.ShowDialog())
            {
                endAlphaNumericUpDown.Value = colorPick.Color.A;
                endRedNumericUpDown.Value = colorPick.Color.R;
                endGreenNumericUpDown.Value = colorPick.Color.G;
                endBlueNumericUpDown.Value = colorPick.Color.B;
                emitters[emitterList.SelectedIndex].flyweight.FEndColorA = (float)(endAlphaNumericUpDown.Value / 255);
                emitters[emitterList.SelectedIndex].flyweight.FEndColorR = (float)(endRedNumericUpDown.Value / 255);
                emitters[emitterList.SelectedIndex].flyweight.FEndColorG = (float)(endGreenNumericUpDown.Value / 255);
                emitters[emitterList.SelectedIndex].flyweight.FEndColorB = (float)(endBlueNumericUpDown.Value / 255);
                endColorButton.BackColor = Color.FromArgb((int)endAlphaNumericUpDown.Value, (int)endRedNumericUpDown.Value, (int)endGreenNumericUpDown.Value, (int)endBlueNumericUpDown.Value);
            }
        }

        private void endAlphaNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            endColorButton.BackColor = Color.FromArgb((int)endAlphaNumericUpDown.Value, (int)endRedNumericUpDown.Value, (int)endGreenNumericUpDown.Value, (int)endBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FEndColorA = (float)(endAlphaNumericUpDown.Value / 255);
        }

        private void endRedNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            endColorButton.BackColor = Color.FromArgb((int)endAlphaNumericUpDown.Value, (int)endRedNumericUpDown.Value, (int)endGreenNumericUpDown.Value, (int)endBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FEndColorR = (float)(endRedNumericUpDown.Value / 255);
        }

        private void endGreenNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            endColorButton.BackColor = Color.FromArgb((int)endAlphaNumericUpDown.Value, (int)endRedNumericUpDown.Value, (int)endGreenNumericUpDown.Value, (int)endBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FEndColorG = (float)(endGreenNumericUpDown.Value / 255);
        }

        private void endBlueNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            endColorButton.BackColor = Color.FromArgb((int)endAlphaNumericUpDown.Value, (int)endRedNumericUpDown.Value, (int)endGreenNumericUpDown.Value, (int)endBlueNumericUpDown.Value);
            emitters[emitterList.SelectedIndex].flyweight.FEndColorB = (float)(endBlueNumericUpDown.Value / 255);
        }

        private void emitterListRemoveButton_Click(object sender, EventArgs e)
        {
            int index = emitterList.SelectedIndex;
            emitterList.Items.RemoveAt(index);
            emitters.RemoveAt(index);
            emitterList.SelectedIndex = index - 1;
        }

        private void emitterListEditButton_Click(object sender, EventArgs e)
        {
            addForm = new AddEditEmitterForm(emitters[emitterList.SelectedIndex].SEmitterName, emitters[emitterList.SelectedIndex].SFileName, emitters[emitterList.SelectedIndex].ImageID, emitters[emitterList.SelectedIndex].flyweight.drawRect, emitters[emitterList.SelectedIndex].flyweight.centerPoint);
            if (DialogResult.OK == addForm.ShowDialog())
            {
                emitters[emitterList.SelectedIndex].ImageID = TM.LoadTexture(emitters[emitterList.SelectedIndex].SFileName);
                if (-1 != emitters[emitterList.SelectedIndex].ImageID)
                {
                    emitters[emitterList.SelectedIndex].SEmitterName = addForm.name;
                    emitters[emitterList.SelectedIndex].flyweight.centerPoint = addForm.point;
                    emitters[emitterList.SelectedIndex].flyweight.drawRect = addForm.rect;
                }
                else
                {
                    MessageBox.Show("File path is not valid.");
                }
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "XML File|*.xml";
            save.DefaultExt = "xml";

            if (DialogResult.OK == save.ShowDialog())
            {
                XElement xRoot = new XElement("Emitters");

                for (int i = 0; i < emitters.Count; ++i)
                {
                    XElement xEmitter = new XElement("Emitter");
                    xRoot.Add(xEmitter);

                    XAttribute xEmitterName = new XAttribute("EmitterName", emitters[i].SEmitterName);
                    xEmitter.Add(xEmitterName);

                    XAttribute xEmitterFile = new XAttribute("EmitterFile", emitters[i].SFileName);
                    xEmitter.Add(xEmitterFile);

                    XElement xEmitterNormal = new XElement("Normal");
                    xEmitter.Add(xEmitterNormal);

                    XAttribute xEmitterNormalX = new XAttribute("X", emitters[i].emitterNormal.X);
                    xEmitterNormal.Add(xEmitterNormalX);

                    XAttribute xEmitterNormalY = new XAttribute("Y", emitters[i].emitterNormal.Y);
                    xEmitterNormal.Add(xEmitterNormalY);

                    XAttribute xEmitterMaxParticles = new XAttribute("MaxParticles", emitters[i].IMaxParticles);
                    xEmitter.Add(xEmitterMaxParticles);

                    XAttribute xEmitterSpawnRate = new XAttribute("SpawnRate", emitters[i].FSpawnRate);
                    xEmitter.Add(xEmitterSpawnRate);

                    XAttribute xEmitterRepeat = new XAttribute("Repeat", emitters[i].BRepeat);
                    xEmitter.Add(xEmitterRepeat);

                    //--------------------------------------------------------------

                    XElement xFlyweight = new XElement("Flyweight");
                    xEmitter.Add(xFlyweight);

                    XAttribute xFlyweightType = new XAttribute("EmitterType", emitters[i].flyweight.IEmitterType);
                    xFlyweight.Add(xFlyweightType);

                    XAttribute xFlyweightAngle = new XAttribute("EmitterAngle", emitters[i].flyweight.FAngle);
                    xFlyweight.Add(xFlyweightAngle);

                    XElement xFlyweightPosition = new XElement("Position");
                    xFlyweight.Add(xFlyweightPosition);

                    XAttribute xFlyweightPosX = new XAttribute("X", emitters[i].flyweight.Position.X);
                    xFlyweightPosition.Add(xFlyweightPosX);

                    XAttribute xFlyweightPosY = new XAttribute("Y", emitters[i].flyweight.Position.Y);
                    xFlyweightPosition.Add(xFlyweightPosY);

                    XElement xFlyweightDrawRect = new XElement("DrawRect");
                    xFlyweight.Add(xFlyweightDrawRect);

                    XAttribute xFlyweightRectLeft = new XAttribute("Left", emitters[i].flyweight.drawRect.Left);
                    xFlyweightDrawRect.Add(xFlyweightRectLeft);

                    XAttribute xFlyweightRectTop = new XAttribute("Top", emitters[i].flyweight.drawRect.Top);
                    xFlyweightDrawRect.Add(xFlyweightRectTop);

                    XAttribute xFlyweightRectWidth = new XAttribute("Width", emitters[i].flyweight.drawRect.Width);
                    xFlyweightDrawRect.Add(xFlyweightRectWidth);

                    XAttribute xFlyweightRectHeight = new XAttribute("Height", emitters[i].flyweight.drawRect.Height);
                    xFlyweightDrawRect.Add(xFlyweightRectHeight);

                    XElement xFlyweightCenter = new XElement("CenterPoint");
                    xFlyweight.Add(xFlyweightCenter);

                    XAttribute xFlyweightCenterX = new XAttribute("X", emitters[i].flyweight.centerPoint.X);
                    xFlyweightCenter.Add(xFlyweightCenterX);

                    XAttribute xFlyweightCenterY = new XAttribute("Y", emitters[i].flyweight.centerPoint.Y);
                    xFlyweightCenter.Add(xFlyweightCenterY);

                    XAttribute xFlyweightWidth = new XAttribute("EmitterWidth", emitters[i].flyweight.IEmitterWidth);
                    xFlyweight.Add(xFlyweightWidth);

                    XAttribute xFlyweightHeight = new XAttribute("EmitterHeight", emitters[i].flyweight.IEmitterHeight);
                    xFlyweight.Add(xFlyweightHeight);

                    XAttribute xFlyweightRange = new XAttribute("EmitterRange", emitters[i].flyweight.IEmitterRange);
                    xFlyweight.Add(xFlyweightRange);

                    XAttribute xFlyweightMinLife = new XAttribute("EmitterMinLife", emitters[i].flyweight.FMinLife);
                    xFlyweight.Add(xFlyweightMinLife);

                    XAttribute xFlyweightMaxLife = new XAttribute("EmitterMaxLife", emitters[i].flyweight.FMaxLife);
                    xFlyweight.Add(xFlyweightMaxLife);

                    XElement xFlyweightStartColor = new XElement("StartColor");
                    xFlyweight.Add(xFlyweightStartColor);

                    XAttribute xFlyweightStartColorA = new XAttribute("A", emitters[i].flyweight.FStartColorA);
                    xFlyweightStartColor.Add(xFlyweightStartColorA);

                    XAttribute xFlyweightStartColorR = new XAttribute("R", emitters[i].flyweight.FStartColorR);
                    xFlyweightStartColor.Add(xFlyweightStartColorR);

                    XAttribute xFlyweightStartColorG = new XAttribute("G", emitters[i].flyweight.FStartColorG);
                    xFlyweightStartColor.Add(xFlyweightStartColorG);

                    XAttribute xFlyweightStartColorB = new XAttribute("B", emitters[i].flyweight.FStartColorB);
                    xFlyweightStartColor.Add(xFlyweightStartColorB);

                    XElement xFlyweightEndColor = new XElement("EndColor");
                    xFlyweight.Add(xFlyweightEndColor);

                    XAttribute xFlyweightEndColorA = new XAttribute("A", emitters[i].flyweight.FEndColorA);
                    xFlyweightEndColor.Add(xFlyweightEndColorA);

                    XAttribute xFlyweightEndColorR = new XAttribute("R", emitters[i].flyweight.FEndColorR);
                    xFlyweightEndColor.Add(xFlyweightEndColorR);

                    XAttribute xFlyweightEndColorG = new XAttribute("G", emitters[i].flyweight.FEndColorG);
                    xFlyweightEndColor.Add(xFlyweightEndColorG);

                    XAttribute xFlyweightEndColorB = new XAttribute("B", emitters[i].flyweight.FEndColorB);
                    xFlyweightEndColor.Add(xFlyweightEndColorB);

                    XAttribute xFlyweightStartScale = new XAttribute("StartScale", emitters[i].flyweight.FStartScale);
                    xFlyweight.Add(xFlyweightStartScale);

                    XAttribute xFlyweightEndScale = new XAttribute("EndScale", emitters[i].flyweight.FEndScale);
                    xFlyweight.Add(xFlyweightEndScale);

                    XAttribute xFlyweightStartSpeed = new XAttribute("StartSpeed", emitters[i].flyweight.FStartSpeed);
                    xFlyweight.Add(xFlyweightStartSpeed);

                    XAttribute xFlyweightEndSpeed = new XAttribute("EndSpeed", emitters[i].flyweight.FEndSpeed);
                    xFlyweight.Add(xFlyweightEndSpeed);

                    XAttribute xFlyweightWeight = new XAttribute("ParticleWeight", emitters[i].flyweight.FParticleMass);
                    xFlyweight.Add(xFlyweightWeight);

                    XAttribute xFlyweightGravity = new XAttribute("Gravity", emitters[i].flyweight.BGravity);
                    xFlyweight.Add(xFlyweightGravity);

                    XAttribute xFlyweightWind = new XAttribute("Wind", emitters[i].flyweight.BWind);
                    xFlyweight.Add(xFlyweightWind);
                }

                xRoot.Save(save.FileName);
                SOpenFile = save.FileName;
            }
        }

        private void normalNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (normalNumericUpDown.Value == (decimal)-1)
                normalNumericUpDown.Value = (decimal)359.9;
            else if (normalNumericUpDown.Value == (decimal)360.0)
                normalNumericUpDown.Value = (decimal)1;

            Vector n = new Vector(0, -1);
            n.Rotate((float)normalNumericUpDown.Value);

            emitters[emitterList.SelectedIndex].emitterNormal = n;
            emitters[emitterList.SelectedIndex].flyweight.FAngle = (float)normalNumericUpDown.Value;
        }

        private void systemPathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                SSystemPath = dlg.SelectedPath;
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "XML Files|*.xml";

            if (DialogResult.OK == open.ShowDialog())
            {
                newToolStripMenuItem_Click(sender, e);
                
                SOpenFile = open.FileName;

                XElement xRoot = XElement.Load(open.FileName);


                IEnumerable<XElement> xEmitters = xRoot.Elements("Emitter");

                foreach (XElement xEmitter in xEmitters)
                {
                    XAttribute xEmitterName = xEmitter.Attribute("EmitterName");

                    XAttribute xEmitterFile = xEmitter.Attribute("EmitterFile");

                    XElement xEmitterNormal = xEmitter.Element("Normal");

                    XAttribute xNormalX = xEmitterNormal.Attribute("X");

                    XAttribute xNormalY = xEmitterNormal.Attribute("Y");

                    XAttribute xEmitterMaxParticles = xEmitter.Attribute("MaxParticles");

                    XAttribute xEmitterSpawnRate = xEmitter.Attribute("SpawnRate");

                    XAttribute xEmitterRepeat = xEmitter.Attribute("Repeat");

                    XElement xFlyweight = xEmitter.Element("Flyweight");

                    XAttribute xFlyweightType = xFlyweight.Attribute("EmitterType");

                    XAttribute xFlyweightAngle = xFlyweight.Attribute("EmitterAngle");

                    XElement xFlyweightPosition = xFlyweight.Element("Position");

                    XAttribute xFlyweightPosX = xFlyweightPosition.Attribute("X");

                    XAttribute xFlyweightPosY = xFlyweightPosition.Attribute("Y");

                    XElement xFlyweightDrawRect = xFlyweight.Element("DrawRect");

                    XAttribute xFlyweightRectLeft = xFlyweightDrawRect.Attribute("Left");

                    XAttribute xFlyweightRectTop = xFlyweightDrawRect.Attribute("Top");

                    XAttribute xFlyweightRectWidth = xFlyweightDrawRect.Attribute("Width");

                    XAttribute xFlyweightRectHeight = xFlyweightDrawRect.Attribute("Height");

                    XElement xFlyweightCenter = xFlyweight.Element("CenterPoint");

                    XAttribute xFlyweightCenterX = xFlyweightCenter.Attribute("X");

                    XAttribute xFlyweightCenterY = xFlyweightCenter.Attribute("Y");

                    XAttribute xFlyweightWidth = xFlyweight.Attribute("EmitterWidth");

                    XAttribute xFlyweightHeight = xFlyweight.Attribute("EmitterHeight");

                    XAttribute xFlyweightRange = xFlyweight.Attribute("EmitterRange");

                    XAttribute xFlyweightMinLife = xFlyweight.Attribute("EmitterMinLife");

                    XAttribute xFlyweightMaxLife = xFlyweight.Attribute("EmitterMaxLife");

                    XElement xFlyweightStartColor = xFlyweight.Element("StartColor");

                    XAttribute xFlyweightStartColorA = xFlyweightStartColor.Attribute("A");

                    XAttribute xFlyweightStartColorR = xFlyweightStartColor.Attribute("R");

                    XAttribute xFlyweightStartColorG = xFlyweightStartColor.Attribute("G");

                    XAttribute xFlyweightStartColorB = xFlyweightStartColor.Attribute("B");

                    XElement xFlyweightEndColor = xFlyweight.Element("EndColor");

                    XAttribute xFlyweightEndColorA = xFlyweightEndColor.Attribute("A");

                    XAttribute xFlyweightEndColorR = xFlyweightEndColor.Attribute("R");

                    XAttribute xFlyweightEndColorG = xFlyweightEndColor.Attribute("G");

                    XAttribute xFlyweightEndColorB = xFlyweightEndColor.Attribute("B");

                    XAttribute xFlyweightStartScale = xFlyweight.Attribute("StartScale");

                    XAttribute xFlyweightEndScale = xFlyweight.Attribute("EndScale");

                    XAttribute xFlyweightStartSpeed = xFlyweight.Attribute("StartSpeed");

                    XAttribute xFlyweightEndSpeed = xFlyweight.Attribute("EndSpeed");

                    XAttribute xFlyweightWeight = xFlyweight.Attribute("ParticleWeight");

                    XAttribute xFlyweightGravity = xFlyweight.Attribute("Gravity");

                    XAttribute xFlyweightWind = xFlyweight.Attribute("Wind");

                    //-------------

                    Emitter E = new Emitter(xEmitterFile.Value.ToString(), xEmitterName.Value.ToString());
                    E.emitterNormal = new Vector(float.Parse(xNormalX.Value), float.Parse(xNormalY.Value));
                    E.IMaxParticles = int.Parse(xEmitterMaxParticles.Value);
                    E.FSpawnRate = float.Parse(xEmitterSpawnRate.Value);
                    E.BRepeat = bool.Parse(xEmitterRepeat.Value);
                    E.flyweight.IEmitterType = int.Parse(xFlyweightType.Value);
                    E.flyweight.FAngle = float.Parse(xFlyweightAngle.Value);
                    E.flyweight.Position = new PointF(float.Parse(xFlyweightPosX.Value), float.Parse(xFlyweightPosY.Value));
                    E.flyweight.drawRect = new Rectangle(int.Parse(xFlyweightRectLeft.Value), int.Parse(xFlyweightRectTop.Value), int.Parse(xFlyweightRectWidth.Value), int.Parse(xFlyweightRectHeight.Value));
                    E.flyweight.centerPoint = new PointF(float.Parse(xFlyweightCenterX.Value), float.Parse(xFlyweightCenterY.Value));
                    E.flyweight.IEmitterWidth = int.Parse(xFlyweightWidth.Value);
                    E.flyweight.IEmitterHeight = int.Parse(xFlyweightHeight.Value);
                    E.flyweight.IEmitterRange = int.Parse(xFlyweightRange.Value);
                    E.flyweight.FMinLife = float.Parse(xFlyweightMinLife.Value);
                    E.flyweight.FMaxLife = float.Parse(xFlyweightMaxLife.Value);
                    E.flyweight.FStartColorA = float.Parse(xFlyweightStartColorA.Value);
                    E.flyweight.FStartColorR = float.Parse(xFlyweightStartColorR.Value);
                    E.flyweight.FStartColorG = float.Parse(xFlyweightStartColorG.Value);
                    E.flyweight.FStartColorB = float.Parse(xFlyweightStartColorB.Value);
                    E.flyweight.FEndColorA = float.Parse(xFlyweightEndColorA.Value);
                    E.flyweight.FEndColorR = float.Parse(xFlyweightEndColorR.Value);
                    E.flyweight.FEndColorG = float.Parse(xFlyweightEndColorG.Value);
                    E.flyweight.FEndColorB = float.Parse(xFlyweightEndColorB.Value);
                    E.flyweight.FStartScale = float.Parse(xFlyweightStartScale.Value);
                    E.flyweight.FEndScale = float.Parse(xFlyweightEndScale.Value);
                    E.flyweight.FStartSpeed = float.Parse(xFlyweightStartSpeed.Value);
                    E.flyweight.FEndSpeed = float.Parse(xFlyweightEndSpeed.Value);
                    E.flyweight.FParticleMass = float.Parse(xFlyweightWeight.Value);
                    E.flyweight.BGravity = bool.Parse(xFlyweightGravity.Value);
                    E.flyweight.BWind = bool.Parse(xFlyweightWind.Value);
                    E.ImageID = TM.LoadTexture(SSystemPath + "\\" + E.SFileName);
                    emitters.Add(E);
                    emitterList.Items.Add(new Item(E.SEmitterName, E));
                }
                if (emitterList.Items.Count > 0)
                {
                    emitterList.SelectedIndex = 0;
                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(SOpenFile))
            {
                XElement xRoot = new XElement("Emitters");

                for (int i = 0; i < emitters.Count; ++i)
                {
                    XElement xEmitter = new XElement("Emitter");
                    xRoot.Add(xEmitter);

                    XAttribute xEmitterName = new XAttribute("EmitterName", emitters[i].SEmitterName);
                    xEmitter.Add(xEmitterName);

                    XAttribute xEmitterFile = new XAttribute("EmitterFile", emitters[i].SFileName);
                    xEmitter.Add(xEmitterFile);

                    XElement xEmitterNormal = new XElement("Normal");
                    xEmitter.Add(xEmitterNormal);

                    XAttribute xEmitterNormalX = new XAttribute("X", emitters[i].emitterNormal.X);
                    xEmitterNormal.Add(xEmitterNormalX);

                    XAttribute xEmitterNormalY = new XAttribute("Y", emitters[i].emitterNormal.Y);
                    xEmitterNormal.Add(xEmitterNormalY);

                    XAttribute xEmitterMaxParticles = new XAttribute("MaxParticles", emitters[i].IMaxParticles);
                    xEmitter.Add(xEmitterMaxParticles);

                    XAttribute xEmitterSpawnRate = new XAttribute("SpawnRate", emitters[i].FSpawnRate);
                    xEmitter.Add(xEmitterSpawnRate);

                    XAttribute xEmitterRepeat = new XAttribute("Repeat", emitters[i].BRepeat);
                    xEmitter.Add(xEmitterRepeat);

                    //--------------------------------------------------------------

                    XElement xFlyweight = new XElement("Flyweight");
                    xEmitter.Add(xFlyweight);

                    XAttribute xFlyweightType = new XAttribute("EmitterType", emitters[i].flyweight.IEmitterType);
                    xFlyweight.Add(xFlyweightType);

                    XAttribute xFlyweightAngle = new XAttribute("EmitterAngle", emitters[i].flyweight.FAngle);
                    xFlyweight.Add(xFlyweightAngle);

                    XElement xFlyweightPosition = new XElement("Position");
                    xFlyweight.Add(xFlyweightPosition);

                    XAttribute xFlyweightPosX = new XAttribute("X", emitters[i].flyweight.Position.X);
                    xFlyweightPosition.Add(xFlyweightPosX);

                    XAttribute xFlyweightPosY = new XAttribute("Y", emitters[i].flyweight.Position.Y);
                    xFlyweightPosition.Add(xFlyweightPosY);

                    XElement xFlyweightDrawRect = new XElement("DrawRect");
                    xFlyweight.Add(xFlyweightDrawRect);

                    XAttribute xFlyweightRectLeft = new XAttribute("Left", emitters[i].flyweight.drawRect.Left);
                    xFlyweightDrawRect.Add(xFlyweightRectLeft);

                    XAttribute xFlyweightRectTop = new XAttribute("Top", emitters[i].flyweight.drawRect.Top);
                    xFlyweightDrawRect.Add(xFlyweightRectTop);

                    XAttribute xFlyweightRectWidth = new XAttribute("Width", emitters[i].flyweight.drawRect.Width);
                    xFlyweightDrawRect.Add(xFlyweightRectWidth);

                    XAttribute xFlyweightRectHeight = new XAttribute("Height", emitters[i].flyweight.drawRect.Height);
                    xFlyweightDrawRect.Add(xFlyweightRectHeight);

                    XElement xFlyweightCenter = new XElement("CenterPoint");
                    xFlyweight.Add(xFlyweightCenter);

                    XAttribute xFlyweightCenterX = new XAttribute("X", emitters[i].flyweight.centerPoint.X);
                    xFlyweightCenter.Add(xFlyweightCenterX);

                    XAttribute xFlyweightCenterY = new XAttribute("Y", emitters[i].flyweight.centerPoint.Y);
                    xFlyweightCenter.Add(xFlyweightCenterY);

                    XAttribute xFlyweightWidth = new XAttribute("EmitterWidth", emitters[i].flyweight.IEmitterWidth);
                    xFlyweight.Add(xFlyweightWidth);

                    XAttribute xFlyweightHeight = new XAttribute("EmitterHeight", emitters[i].flyweight.IEmitterHeight);
                    xFlyweight.Add(xFlyweightHeight);

                    XAttribute xFlyweightRange = new XAttribute("EmitterRange", emitters[i].flyweight.IEmitterRange);
                    xFlyweight.Add(xFlyweightRange);

                    XAttribute xFlyweightMinLife = new XAttribute("EmitterMinLife", emitters[i].flyweight.FMinLife);
                    xFlyweight.Add(xFlyweightMinLife);

                    XAttribute xFlyweightMaxLife = new XAttribute("EmitterMaxLife", emitters[i].flyweight.FMaxLife);
                    xFlyweight.Add(xFlyweightMaxLife);

                    XElement xFlyweightStartColor = new XElement("StartColor");
                    xFlyweight.Add(xFlyweightStartColor);

                    XAttribute xFlyweightStartColorA = new XAttribute("A", emitters[i].flyweight.FStartColorA);
                    xFlyweightStartColor.Add(xFlyweightStartColorA);

                    XAttribute xFlyweightStartColorR = new XAttribute("R", emitters[i].flyweight.FStartColorR);
                    xFlyweightStartColor.Add(xFlyweightStartColorR);

                    XAttribute xFlyweightStartColorG = new XAttribute("G", emitters[i].flyweight.FStartColorG);
                    xFlyweightStartColor.Add(xFlyweightStartColorG);

                    XAttribute xFlyweightStartColorB = new XAttribute("B", emitters[i].flyweight.FStartColorB);
                    xFlyweightStartColor.Add(xFlyweightStartColorB);

                    XElement xFlyweightEndColor = new XElement("EndColor");
                    xFlyweight.Add(xFlyweightEndColor);

                    XAttribute xFlyweightEndColorA = new XAttribute("A", emitters[i].flyweight.FEndColorA);
                    xFlyweightEndColor.Add(xFlyweightEndColorA);

                    XAttribute xFlyweightEndColorR = new XAttribute("R", emitters[i].flyweight.FEndColorR);
                    xFlyweightEndColor.Add(xFlyweightEndColorR);

                    XAttribute xFlyweightEndColorG = new XAttribute("G", emitters[i].flyweight.FEndColorG);
                    xFlyweightEndColor.Add(xFlyweightEndColorG);

                    XAttribute xFlyweightEndColorB = new XAttribute("B", emitters[i].flyweight.FEndColorB);
                    xFlyweightEndColor.Add(xFlyweightEndColorB);

                    XAttribute xFlyweightStartScale = new XAttribute("StartScale", emitters[i].flyweight.FStartScale);
                    xFlyweight.Add(xFlyweightStartScale);

                    XAttribute xFlyweightEndScale = new XAttribute("EndScale", emitters[i].flyweight.FEndScale);
                    xFlyweight.Add(xFlyweightEndScale);

                    XAttribute xFlyweightStartSpeed = new XAttribute("StartSpeed", emitters[i].flyweight.FStartSpeed);
                    xFlyweight.Add(xFlyweightStartSpeed);

                    XAttribute xFlyweightEndSpeed = new XAttribute("EndSpeed", emitters[i].flyweight.FEndSpeed);
                    xFlyweight.Add(xFlyweightEndSpeed);

                    XAttribute xFlyweightWeight = new XAttribute("ParticleWeight", emitters[i].flyweight.FParticleMass);
                    xFlyweight.Add(xFlyweightWeight);

                    XAttribute xFlyweightGravity = new XAttribute("Gravity", emitters[i].flyweight.BGravity);
                    xFlyweight.Add(xFlyweightGravity);

                    XAttribute xFlyweightWind = new XAttribute("Wind", emitters[i].flyweight.BWind);
                    xFlyweight.Add(xFlyweightWind);
                }

                xRoot.Save(SOpenFile);
            }
            else
            {
                saveAsToolStripMenuItem_Click(sender, e);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            emitters.Clear();
            emitterList.Items.Clear();
            emitterList.SelectedIndex = -1;
            emitterList.Text = "";
        }

    }


}
